package com.ruoyi.web.controller.image;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringMD5Checksum;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
// 使用@RequestMapping注解定义基础路径
@RequestMapping("/image/demo")
public class DemoImgController {
    private String DEMO_IMAGE(){
        return  RuoYiConfig.getProfile() +  System.getProperty("file.separator") + "demoImg" + System.getProperty("file.separator") ;
    }

    private static Map<String,String> FOLDER_PATH = new HashMap<>();

    @RequestMapping(value = "/allFile/{folderId}",method = RequestMethod.POST)
    public R<JSONArray> allFile(@PathVariable String folderId) throws IOException {
        JSONArray re = new JSONArray();
        File[] imgArr = new File(FOLDER_PATH.get(folderId)).listFiles();
        for(File imgObjTemp:imgArr){
            JSONObject imgTemp = new JSONObject();
            String fileId = StringMD5Checksum.getStringMD5Checksum(imgObjTemp.getAbsolutePath());
            imgTemp.put("fileId", fileId);
            imgTemp.put("url", "data:image/jpeg;base64," + encodeFileToBase64Binary(imgObjTemp.getAbsolutePath()));
            re.add(imgTemp);
        }
        return R.ok(re);
    }


    @RequestMapping(value = "/allFolder",method = RequestMethod.POST)
    public R<JSONArray> allFolder() throws IOException {
        JSONArray re = new JSONArray();
        File folder = new File(DEMO_IMAGE());
        File[] folderAll = folder.listFiles();
        for(int i = folderAll.length - 1 ; i >= 0 ; i--){
            File dateFolder = folderAll[i];
            JSONObject dataTemp = new JSONObject();
            dataTemp.put("folderName",dateFolder.getName());
            JSONArray sonArr = new JSONArray();
            File[] imgFolderAll = dateFolder.listFiles();
            for(File imgFolder:imgFolderAll){
                JSONObject imgFolderTemp = new JSONObject();
                String folderId = StringMD5Checksum.getStringMD5Checksum(imgFolder.getAbsolutePath());
                imgFolderTemp.put("folderName",imgFolder.getName());
                imgFolderTemp.put("folderId",folderId);
                FOLDER_PATH.put(folderId,imgFolder.getAbsolutePath());
                sonArr.add(imgFolderTemp);
                //break;
            }
            dataTemp.put("sonList",sonArr);
            dataTemp.put("loadOk",false);
            dataTemp.put("srcList",new JSONArray());
            re.add(dataTemp);
            //break;
        }
        return R.ok(re);
    }



    /**
     * 将文件转换为Base64编码的字符串
     *
     * @param filePath 文件路径
     * @return Base64编码的字符串
     */
    public static String encodeFileToBase64Binary(String filePath) {
        try (FileInputStream fileInputStreamReader = new FileInputStream(new File(filePath))) {
            byte[] bytes = new byte[(int) new File(filePath).length()];
            fileInputStreamReader.read(bytes);
            return Base64.getEncoder().encodeToString(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }



}
